package easy;

public class Solution_1991 {

    public int findMiddleIndex(int[] nums) {
        int sum = 0;
        for (int num : nums) {
            sum += num;
        }
        int left = 0;
        for (int i = 0; i < nums.length; i++) {
            if ((left << 1) + nums[i] == sum) {
                return i;
            }
            left += nums[i];
        }
        return -1;
    }

    public static void main(String[] args) {
        Solution_1991 model = new Solution_1991();
        System.out.println(model.findMiddleIndex(new int[]{2, 3, -1, 8, 4}));
        System.out.println(model.findMiddleIndex(new int[]{1, -1, 4}));
        System.out.println(model.findMiddleIndex(new int[]{2, 5}));
        System.out.println(model.findMiddleIndex(new int[]{21}));
    }
}
